package cn.gailvlun.gll.presentation.chat;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityRoomDetailBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.ListRes;
import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.apartment.room.Room;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.adapter.NoScrollLayoutManager;
import cn.gailvlun.gll.presentation.chat.adapter.RoomDetailAdapter;
import cn.gailvlun.gll.presentation.chat.vm.RoomDetailViewModel;
import cn.gailvlun.gll.presentation.chat.vm.RoomDetailViewModelFactory;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class RoomDetailActivity extends BaseActivity {

    public static Observable<Result<Activity>> openActivity(Activity context, int roomId) {
        Intent intent = new Intent(context, RoomDetailActivity.class);
        intent.putExtra("roomId", roomId);

        return RxActivityResult.on(context).startIntent(intent);
    }

    private ActivityRoomDetailBinding mBinding;
    private RoomDetailAdapter mAdapter;
    private NoScrollLayoutManager mLayoutManager;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_room_detail);
        mBinding.setPresenter(new Presenter());

        mAdapter = new RoomDetailAdapter(mContext);
        mBinding.rvRoomMember.setAdapter(mAdapter);

        mLayoutManager = new NoScrollLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mBinding.rvRoomMember.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    private RoomDetailViewModel mViewModel;

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        int roomId = getIntent().getIntExtra("roomId", -1);

        mViewModel = ViewModelProviders.of(this, new RoomDetailViewModelFactory(roomId)).get(RoomDetailViewModel.class);
        mBinding.setItem(mViewModel.getRoom());
        mBinding.setMember(mViewModel.getMember());

        HttpMethods.getMemberService()
                .getMemberList(mViewModel.getRoomId(), true, null, null, 10)
                .compose(RxUtil.<ListRes<Member>>applyScheduler())
                .subscribe(new HttpSubscriber<ListRes<Member>>() {
                    @Override
                    protected void onSuccess(ListRes<Member> response) {
                        if (!IsEmpty.list(response.getResults())) {
                            mAdapter.set(response.getResults());
                        }
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable ListRes<Member> response, int code) {
                        DialogUtil.showError(mContext, errMsg);
                    }
                });
    }

    public class Presenter {
        public void clickRoomMember(Room room) {
            RoomMemberActivity.openActivity(mContext, room.getId());
        }

        public void onNickName(Room room) {
            ModifyNickActivity.openActivity(mContext, room.getId())
                    .subscribe(new Consumer<Result<Activity>>() {
                        @Override
                        public void accept(Result<Activity> activityResult) throws Exception {
                            if (activityResult.resultCode() == RESULT_OK) {
                                mViewModel.updateMember();
                                mBinding.setMember(mViewModel.getMember());
                            }
                        }
                    });
        }

        public void silence(Room room) {
            SilenceMemberActivity.Companion.openActivity(mContext, room.getId());
        }

        public void onDeleteRoomMessage(Room room) {

        }
    }

    public static class Util {
        public static int getBlockVisibility(Room room, Member myMember) {
            if (room.getOwner().getId() == myMember.getOwner().getId()) {
                return View.VISIBLE;
            }
            return View.GONE;
        }
    }
}
